import 'ns_http_manager.dart';
import 'ns_result_data.dart';

extension NSHttpExtension on String {
  ///Get请求
  Future<NSResponseData> get(
    Map<String, dynamic> params, {
    withLoading = false,
    isJson = true,
    hideMsg = false,
    isNeedCommonParams = true,
  }) async {
    String url = this;
    dynamic result = await NSHttpManager().get(url, params: params, hideMsg: hideMsg);
    if (result is NSResponseData) {
      return result;
    }
    return NSResponseData(code: "-1", data: {}, msg: "数据出现点问题");
  }

  ///Post请求
  Future<NSResponseData> post(
    params, {
    withLoading = false,
    isJson = true,
    hideMsg = false,
    isNeedCommonParams = true,
  }) async {
    String url = this;
    dynamic result = await NSHttpManager().post(
      url,
      params: params,
      isJson: isJson,
      hideMsg: hideMsg,
      isNeedCommonParams: isNeedCommonParams,
    );
    if (result is NSResponseData) {
      return result;
    }
    // if (GlobalConfig.isDebug) {
    //   throw UnsupportedError("统一返回值类型应该为ResponseData,当前类型为:${result.runtimeType},${result.toString()},需处理下");
    // }
    return NSResponseData(code: "-1", data: {}, msg: "数据出现点问题");
  }

  ///Delete请求
  Future<NSResponseData> delete(
    Map<String, dynamic> params, {
    withLoading = false,
    hideMsg = false,
  }) async {
    String url = this;
    dynamic result = await NSHttpManager().delete(
      url,
      params: params,
      hideMsg: hideMsg,
    );
    if (result is NSResponseData) {
      return result;
    }
    // if (GlobalConfig.isDebug) {
    //   throw UnsupportedError("统一返回值类型应该为ResponseData,当前类型为:${result.runtimeType},${result.toString()},需处理下");
    // }
    return NSResponseData(code: "-1", data: {}, msg: "数据出现点问题");
  }
}
